<?php
  ob_start();
  session_start();
  error_reporting(0);
  set_time_limit(0);



  require_once("../../includes/dbconfig.php");
  require_once("../includes/functions.php");
  require_once("../includes/settings_apis.php");  
  
  
		mysql_query("SET character_set_results=utf8");
		mysql_query("SET character_set_client=utf8");
        mysql_query("SET character_set_connection=utf8");
  
  

if(!isset($_SESSION[SITE_NAME.'_X_AdMiNCP_XADMINLOGGEDID__XXHST']))
{    header("location: ../login.php");
   	exit;
}


	  
	$smq_1 = mysql_query("SELECT adgate_url FROM networks_settings");
	if(mysql_num_rows($smq_1))
	{
          $smr_1 = mysql_fetch_array($smq_1);		
		  //adgatemedia
		  $url = stripslashes($smr_1['adgate_url']);
		  
		  
	}	  
	  
	  $result = file_get_contents($url);
	  $arrOffers = json_decode(json_encode((array)simplexml_load_string($result)),1);

      if (is_array($arrOffers)){
		
      foreach ($arrOffers AS $IO => $__offers){ 
      foreach ($__offers AS $IO2 => $offer){ 
	  
      $country = $offer['country'];
	  
	  if(!isset($offer['tracking_url']))
	  {
         continue;		  
	  }
	  
	  
	  
	  if((stripos($offer['name'], "Non Incentive") > 0) || (stripos($offer['name'], "NO Incentive") > 0))
	  {
		 continue;  
	  }
	  
	  
	  
	  
      
	  if(in_array($offer['id'], $offers))
	  {
	      $offers[$offer['id']]['country'] = $offers[$offer['id']]['country']."|".$country;
		  continue;
	  }
	  
	  $name = '';
	  $name = $offer['anchor'];
	  $name = trim($name);
	  
	  $desc = '';
	  $desc = $offer['requirements'];
	  $desc = html_entity_decode($desc);
  
	  $desc = str_ireplace("<br>", " ", $desc);
  
	  $offers[$offer['id']]['id'] = $offer['id'];
	  $offers[$offer['id']]['name'] = $name;
	  $offers[$offer['id']]['url'] = $offer['tracking_url'];
	  $offers[$offer['id']]['description'] = $desc;
	  $offers[$offer['id']]['country'] = $country;
	  $offers[$offer['id']]['payout'] = $offer['payout'];
	  $offers[$offer['id']]['type'] = $offer['type'];	  


	  
     }
	  
	 $offer = null; 
	  
	  
    }
    }


     $offer = null; 
     //Delete old gw_offers now
     @mysql_query("DELETE FROM gw_offers WHERE network = 'Adgatemedia'");


    //store offers
    foreach($offers as $camps => $offer)
	{
		?>
       <div style="margin:10px;">
       Name: <?=$offer['name']?><br />
       URL: <?=$offer['url']?><br />
       Description: <?=$offer['description']?><br />
       Country: <?=$offer['country']?>
       </div>		
       <?
	   
	   $campid = $offer['id'];
	   $name = mysql_real_escape_string($offer['name']);
	   $desc = mysql_real_escape_string($offer['description']);
	   $link = makesafe($offer['url']);
	   $status = 1;
	   $credits = makesafe($offer['payout']);
	   $limit = 0;
	   $countries = makesafe($offer['country']);
	   $network = 'Adgatemedia';
	   
	   if($offer['type'] == "cell")
	   {
          $mobile = 1;		   
	   }else
	   {
	     $mobile  = 0;	   
       }
	   
	   if(empty($name))
	   {
 	        continue;	   
	   }
	   
	   //Check if this offer is already in database;
	    if(mysql_num_rows(mysql_query("SELECT * FROM gw_offers WHERE campaign_id = '$campid' AND network = '$network'"))) //check if item is already in db with same name.
		{
            continue;
		}
		
	    if(mysql_num_rows(mysql_query("SELECT * FROM gw_offers WHERE name = '$name'"))) //check if item is already in db with same name.
		{
            continue;
		}			
		
		
		@mysql_query("INSERT INTO gw_offers VALUES(NULL, '$name', '$desc', '$link',  '$status', '$credits', 0,  '$limit', '$countries',   NOW(), '$network', '$campid', '0', '0', '$mobile')");
			   
	   
	   
	}

     header("location: ../index.php?m=gw_offers");
	 exit;
?>